package atomic.atomicreference;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author ZhuQi
 * @ClassName Demo
 * @Date 2022/9/15 13:42
 * @Version 1.0
 * @Description 测试原子引用  AtomicReference
 */
public class Demo {

    public static void main(String[] args) {
        Order order = new Order();
        AtomicReference<Order> orderAtomicReference = new AtomicReference<>(order);

        // cas 原子替换对象
        orderAtomicReference.compareAndSet(order, new Order());

        // cas 原子修改
        orderAtomicReference.getAndUpdate((o)->{
            Order order1 = new Order();
            order1.setName("111");
            return order1;
        });

        // cas 原子修改
        orderAtomicReference.accumulateAndGet(new Order(),(old, o)-> o);

        System.out.println(orderAtomicReference.get());
    }
}
@Data
@Accessors(chain = true)
class Order{
    private String oderNo;

    private String name;
}